﻿using System.Windows;
using System.Windows.Input;
using DevExpress.Xpf.Grid;
using System.Collections.Generic;
using DevExpress.Xpf.Core;
using QuanLyKho.Server;

namespace QuanLyKho
{
    /// <summary>
    /// Interaction logic for TimKiemThuoc.xaml
    /// </summary>
    public partial class frmNhapKhoTimKiemThuoc : DXWindow
    {
        #region Fields

        public int rowCount = 0;

        #endregion 

        #region Properties

        public Dm_Thuoc DmThuocChon { get; set; }

        #endregion

        #region Constructor

        public frmNhapKhoTimKiemThuoc()
        {
            InitializeComponent();

            this.gridDmThuoc.Focus();
            this.tbvThuoc.Focus();
        }

        #endregion

        #region Other Proccessing

        /// <summary>
        /// Search dữ liệu Biệt dược và đổ vào GridView
        /// </summary>
        /// <param name="listDmDCBietDuoc"></param>
        /// <param name="maBietDuoc"></param>
        /// <param name="tenBietDuoc"></param>
        /// <param name="focusColumn">0:ma thuoc, 1:ten thuoc</param>
        public void SearchThuoc(IList<Dm_Thuoc> listDmThuoc, string maThuoc, string tenThuoc, byte focusColumn)
        {
            this.gridDmThuoc.ItemsSource = listDmThuoc;
            if (focusColumn == 0)
            {
                this.tbvThuoc.FocusedColumn = this.gridDmThuoc.Columns["MaThuoc"];
                this.tbvThuoc.FocusedColumn.AutoFilterValue = maThuoc;
            }
            else if (focusColumn == 1)
            {
                this.tbvThuoc.FocusedColumn = this.gridDmThuoc.Columns["TenThuocHamLuong"];
                this.tbvThuoc.FocusedColumn.AutoFilterValue = tenThuoc;
            }

            tbvThuoc.FocusedRowHandle = tbvThuoc.AutoFilterRowData.RowHandle.Value;
            rowCount = gridDmThuoc.VisibleRowCount;
            if (rowCount == 0)
                DmThuocChon = null;
            else if (rowCount == 1)
            {
                gridDmThuoc.View.MoveFirstRow();
                DmThuocChon = gridDmThuoc.GetFocusedRow() as Dm_Thuoc;
            }
        }

        #endregion

        #region Events

        /// <summary>
        /// Event TableView_MouseDoubleClick
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event</param>
        private void TableView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (this.gridDmThuoc.View.GetRowHandleByMouseEventArgs(e) == GridControl.InvalidRowHandle)
                return;

            if (this.gridDmThuoc.GetFocusedRow() == null)
                return;

            DmThuocChon = (Dm_Thuoc)this.gridDmThuoc.GetFocusedRow();
            this.Close();
        }

        /// <summary>
        /// Event TableView_PreviewKeyDown
        /// </summary>
        /// <param name="sender">Sender</param>
        /// <param name="e">Event</param>
        private void TableView_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                if (this.gridDmThuoc.GetFocusedRow() == null)
                    return;

                DmThuocChon = (Dm_Thuoc)this.gridDmThuoc.GetFocusedRow();
                this.Close();
            }
        }

        #endregion

        private void gridDmThuoc_PreviewKeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Escape)
            {
                this.Close();
            }
        }
    }
}
